using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using CPGVertragsverwaltung.ErrorHandling;
using System.Windows.Forms;

namespace CPGVertragsverwaltung.DatabaseAccess
{
    class DBInsertBuilder
    {
        private String mTableName;
        private DataTable mSchemaTable = null;
        StringPairCollection mColumnPairCollection = new StringPairCollection();

        #region Properties
        public StringPairCollection ColumnPairCollection
        {
            get { return mColumnPairCollection; }
            set { mColumnPairCollection = value; }
        }
        #endregion

        public DBInsertBuilder(String pTableName)
        {
            mTableName = pTableName;

            mSchemaTable = DBProvider.getSchemaTable(pTableName);          
        }

        public void addStringPair(StringPair pStringPair)
        {

            mColumnPairCollection.Add(pStringPair);
        }

        public String getInsertString()
        {
            String result = "";
            StringBuilder mySBuilder = new StringBuilder();
            String ColumnsLine = "";
            String ValuesLine = "";

            //Error if mColumnsPair.Count =0
            if (mColumnPairCollection.Count == 0)
            {
                MessageHandling.showError("InsertString kann nicht erstellt werden aufgrund fehlenden Daten!");
                Application.Exit();
            }


            for (int i = 0; i < mColumnPairCollection.Count; i++)
            {
                StringPair myPair = mColumnPairCollection[i];
                ColumnsLine += myPair.Key;

                //Check if Column exists
                if (mSchemaTable.Columns[myPair.Key] == null)
                {
                    MessageHandling.showError("Spalte " + myPair.Key + " von Tabelle " + mTableName + " kann nicht gelesen werden!", true);
                    Application.Exit();
                    return result;
                }

                //Value with or without ' (apostrophe)
                if (mSchemaTable.Columns[myPair.Key].DataType.ToString() == "System.Int32" || mSchemaTable.Columns[myPair.Key].DataType.ToString() == "System.Int64" || mSchemaTable.Columns[myPair.Key].DataType.ToString() == "System.Double" || mSchemaTable.Columns[myPair.Key].DataType.ToString() == "System.Boolean")
                {
                    if (myPair.Value == "")
                        myPair.Value = "NULL";

                    ValuesLine += myPair.Value;
                }
                else
                {
                    myPair.Value = myPair.Value.Replace("'", "''");
                    ValuesLine += "'" + myPair.Value + "'";
                }

                //Insert "," if it is not the last Item
                if (i < mColumnPairCollection.Count - 1)
                {
                    ColumnsLine += ",";
                    ValuesLine += ",";
                }
            }

            String[] agruments ={ mTableName, ColumnsLine, ValuesLine};
            mySBuilder.AppendFormat("INSERT INTO {0}({1}) VALUES ({2})", agruments);

            result = mySBuilder.ToString();
            return result;
        }

    }
}
